<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');

class Menu extends CI_Controller
{
	function __construct()
	{
		parent::__construct();
		// check if user logged in
		if(!$this->ion_auth->logged_in()){
			redirect('auth/login');
		}
		$this->load->library('form_validation');
		$this->load->helper('file');
		$this->load->model('founder/menu_model');
		//$this->output->enable_profiler(TRUE);
	}
	
	function index()
	{
		/*$controllers    = array();
		$dir            = APPPATH.'/controllers/';
        $files          = scandir($dir);

        $controller_files = array_filter($files, function($filename) {
            return (substr(strrchr($filename, '.'), 1)=='php') ? true : false;
        });

        foreach ($controller_files as $filename)
        {
            require_once('./application/controllers/'.$filename);

            $classname = ucfirst(substr($filename, 0, strrpos($filename, '.')));
            $controller = new $classname();
            $methods = get_class_methods($controller);

            foreach ($methods as $method)
            {
                array_push($methods,$method);
            }

            $controller_info = array(
                'filename' => $filename,
                'class_name' => $classname,
                'methods'  => $methods
            );
            array_push($controllers,$controller_info);
        }

        echo('<pre>');
        print_r($controllers);
        echo('</pre>');
		exit;*/		
		$meta['page_title']='Menu Access';
		$data['menu_headings']= $this->menu_model->get_menu_heading();
		$data['parent_menu'] = $this->menu_model->get_menu();
		$data['all_menu'] = $this->menu_model->menulist();
				
		$data['roles'] =  $this->ion_auth_model->groups()->result();
		$data['access'] = $this->menu_model->get_access();
		$data['heading_access'] = $this->menu_model->get_heading_access();
				
		$this->load->view('common/header',$meta);
		$this->load->view('founder/menu',$data);
		$this->load->view('common/footer');
	}
	
	public function create_menu(){
		
		if(!empty($_REQUEST['parents']))
		{
		$insert = array(
		'name'		=>$_REQUEST['name'],
		'url'		=>$_REQUEST['urls'],
		'parent'	=>$_REQUEST['parents'],
		'icon' 		=>$_REQUEST['icon'],
		'heading_id' 	=>$_REQUEST['heading']
		);
		//print_r($insert);
		}
		else {
		$insert = array(
		'name'		=>$_REQUEST['name'],
		'url'		=>$_REQUEST['urls'],
		'icon' 		=>$_REQUEST['icon'],
		'heading_id' 	=>$_REQUEST['heading']
		);
		}
		//print_r($insert);exit;
		$this->menu_model->create_menu($insert);
		//
	}
	
	public function update_menu(){
		if(!empty($_REQUEST['parents']))
		{
		$insert = array(
		'name'		=>$_REQUEST['name'],
		'url'		=>$_REQUEST['urls'],
		'parent'	=>$_REQUEST['parents'],
		'icon' 		=>$_REQUEST['icon'],
		'heading_id' 	=>$_REQUEST['heading']
		);
		//print_r($insert);
		}
		else {
		$insert = array(
		'name'		=>$_REQUEST['name'],
		'url'		=>$_REQUEST['urls'],
		'icon' 		=>$_REQUEST['icon'],
		'heading_id' 	=>$_REQUEST['heading']
		);
		}
		//print_r($insert);exit;
		$this->menu_model->update_menu($insert,$_REQUEST['edit_id']);
		//
		
		
	}
	
	public function grant_access(){
		$menus = $this->input->post('menu_access');
		$heading_menus = $this->input->post('heading_menu_access');
		$this->menu_model->grant_heading_access($heading_menus);
		$this->menu_model->grant_access($menus);
		//echo '<pre>';
		//print_r($m);
		//exit;
		redirect('founder/menu');
		
	}
	
	public function sorting(){
		$action             = mysql_real_escape_string($_POST['action']);
		$updateRecordsArray = $_POST['recordsArray'];
		$this->menu_model->sorting($action,$updateRecordsArray);
	}
	
	/*Create heading menu*/
	function create_heading(){	
	$this->menu_model->create_heading($_REQUEST['name'],$_REQUEST['icon']);
	//echo $_REQUEST['name'];
	}
	
	function getmenu_details(){
		$data['menu_details'] = $this->menu_model->getmenu_details($_REQUEST['id_menu']);
		$meta['page_title']='Menu Access';
		$data['menu_headings']= $this->menu_model->get_menu_heading();
		$data['parent_menu'] = $this->menu_model->get_menu();
		//$data['parent_menu'] = $this->menu_model->menulist();
				
		$data['roles'] =  $this->ion_auth_model->groups()->result();
		$data['access'] = $this->menu_model->get_access();		
		
		$this->load->view('founder/edit_menu',$data);
	
	}
	function main_menu_sort(){
		 $action             = mysql_real_escape_string($_POST['action']);
		$updateRecordsArray = $_POST['recordsArray'];
		//exit;
		$this->menu_model->main_menu_sort($action,$updateRecordsArray);
	}
	
}
?>
